<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="../../common/import.jsp" %>
<%@ taglib uri="/WEB-INF/privilege" prefix="p"%>
<html>
  <head>
     <style type="text/css">
     </style>
</head>
  <body scroll="no">
  <script type="text/javascript">
       jQuery.ajaxSetup({cache: false});
       $(function(){
    	   $('#moduleTable').treegrid({
    		   //title: "模块列表",
    		    rownumbers: true,
				animate: true,
				collapsible: true,
				fitColumns: true,
				url: '${ctx}/sys/module/list.do',
				idField: 'ID',
				treeField: 'MODULE_NAME',
				showFooter: false,
				columns:[[
	                {field:'MODULE_NAME', title:'模块名称' ,width:100, align:'center',
	                	formatter: function(value,row){
					    	if (row != undefined&&row.ICON!=''){
						    	return '<span class="icon ' +row.ICON+ '">&nbsp;</span><span>'+value+'</span>';
					    	} else {
						    	return '';
					    	}
				    	}},
	                {field:'ICON', title:'图标', width:100, align:'center'},
					{field:'URL', title:'模块请求URL', width:100, align:'center'},
					{field:'IS_LEAF', title:'子节点', width:30, align:'center',
						formatter:function(value){
					    	if (value=='1'){
						    	return '是';
					    	} else if(value=='0'){
						    	return '否';
					    	} else {
					    		return '';
					    	}
				    	}},
					{field:'ORDER_INDEX', title:'排序号', width:30, align:'center'},
					{field:'STATUS', title:'状态', width:40, align:'center',
					    formatter:function(value){
					    	if (value=='1'){
						    	return '<span class="icon icon-used">&nbsp;</span>';
					    	} else if(value=='0'){
						    	return '<span class="icon icon-not-used">&nbsp;</span>';
					    	} else {
					    		return '';
					    	}
				    	}
					}
				]]
			});
    	   $('#moduleTable').treegrid({
    		   onClickRow: function(row){
    			   //alert(row.IS_LEAF);
    			   if(row.IS_LEAF=="1"||$('#moduleTable').treegrid("getChildren", row.ID).length<=0){
    				   $("#moduleFunction").datagrid({
    					   url: "${ctx}/sys/module/getFunctionsByModuleId.do",
    					   queryParams:{moduleId: row.ID}
    				   })
    			   }
    		   }
    	   });
    	   $("#moduleFunction").datagrid({
    		   nowrap: false,
    		   triped: false,
    		   fitColumns: true,
    		   //url: '${ctx}/sys/module/list.do',
    		   columns: [[
    		             //{field: 'ck', checkbox: true},
    		             {field: 'name', title: '功能名称', width: 80, align: 'center'},
    		             {field: 'functionCode', title: '功能代码', width: 80, align: 'center'},
    		             {field: 'url', title: '请求URL', width: 100, align: 'center'}
    		            ]],
    		   pagination: false,
    		   rownumbers: true
    	   });
       });
       
       function add(href){
    	   showWindow("添加模块", href);
    	   //location.href = href;
    	   return false;
       }
       function edit(href){
    	   var node = getSelectedRows();
    	   if (node&&node.length==1){
    		   showWindow("修改模块", href+"?id="+node[0].ID);
    		   //location.href = href+"?id="+node.ID;
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
       function del(href){
    	   var rows = getSelectedRows();
    	   if (rows&&rows.length == 1){
    		   deleteConfirm(function(){
    			   $.blockUI();
    			   $.ajax({
		    			  url: href,
		    		      data: {id: rows[0].ID}, 
		    		      type: "POST",
		    		      success: function(data) {
		    		    	  $.unblockUI();
		    		        	if(data.success==true){
		    		        		showSuccessMsg();
		    		        		reloadGrid();
		    		        	}else{
		    		        		showFailAlert(data.msg)
		    		        	}
		    		      },
		    		      error: function(xhr) {
		    		    	  $.unblockUI();
		    		    	  showFailAlert2(xhr);
		    		      }
		    		  })
    		   });
    	   }else{
    		   showAlertMsg();
    	   }
    	   return false;
       }
       function reloadGrid(){
    	   $("#moduleTable").treegrid("reload");
       }
       function getSelectedRows(){
   		   var rows = $('#moduleTable').treegrid('getSelections');
   		   return rows;
       }
    </script>
    <div class="easyui-layout" data-options="fit:true">
    <div region="north" style="overflow-y: hidden; overflow-x: hidden" border="false">
	    <div class="btabs">
	    	<p:op code="sysModuleAdd">
	    		<a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/module/toAdd.do" 
	    		onclick="return add(this.href);"><span class="icon icon-add">&nbsp;</span>添加</a> 
	    	</p:op>
	    	<p:op code="sysModuleEdit">
	    		<a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/module/toAdd.do" 
	    		onclick="return edit(this.href);"><span class="icon icon-edit">&nbsp;</span>修改</a>
	    	</p:op>
	    	<p:op code="sysModuleDel">
				<a class="easyui-linkbutton" data-options="plain:true" href="${ctx}/sys/module/delete.do" 
				onclick="return del(this.href);"><span class="icon icon-delete">&nbsp;</span>删除</a>
			</p:op>
	    </div>
	</div>
    <div region="center" style="padding: 1px;" title="模块列表" border="false">
       <table id="moduleTable" fit="true"></table>
    </div>
    <div region="east" split="true" hide="true" title="功能列表" style="padding: 1px; width:520px;" border="false">
       <table id="moduleFunction" fit="true"></table>
    </div>
    </div>
  </body>
</html>
